package com.iot.system.core.impl;


import com.iot.common.enums.DeviceStatusType;
import com.iot.common.utils.DateUtils;
import com.iot.common.utils.RedisUtil;
import com.iot.system.core.CoreSystemHomeService;
import com.iot.system.domain.DeviceDetails;
import com.iot.system.domain.vo.DeviceDataCollectionVolumeVo;
import com.iot.system.domain.vo.DeviceTotalVo;
import com.iot.system.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class CoreSystemHomeServiceImpl implements CoreSystemHomeService {

    @Resource
    private IDeviceDetailsService iDeviceDetailsService;
    @Resource
    private IDeviceStateService iDeviceStateService;
    @Resource
    private IDeviceStateLogsService iDeviceStateLogsService;
    @Resource
    private IDeviceEnergyService iDeviceEnergyService;
    @Resource
    private IDeviceParameterRecordService iDeviceParameterRecordService;
    @Resource
    private IDeviceYieldsService iDeviceYieldsService;


    @Override
    public DeviceTotalVo getDeviceAccessTotal() {
        DeviceTotalVo deviceTotalVo = new DeviceTotalVo();
        Integer deviceAccessTotal =iDeviceDetailsService.getDeviceAccessTotal();
        List<Integer> deviceStateList = new ArrayList<>();
        deviceStateList.add(DeviceStatusType.BOOT_UP.getNumber());
        deviceStateList.add(DeviceStatusType.DEVICE_RUN.getNumber());
        deviceStateList.add(DeviceStatusType.DEVICE_RUN_STOP.getNumber());
        deviceStateList.add(DeviceStatusType.FAILURE.getNumber());
        deviceStateList.add(DeviceStatusType.STOP_FAILURE.getNumber());
        Integer deviceOlineTotal = iDeviceStateService.getDeviceOlineStateTotal(deviceStateList);
        deviceStateList.clear();
        deviceStateList.add(DeviceStatusType.FAILURE.getNumber());
        Integer deviceAlarmTotal = iDeviceStateService.getDeviceOlineStateTotal(deviceStateList);
        deviceTotalVo.setDeviceAccessTotal(deviceAccessTotal);
        deviceTotalVo.setDeviceOnlineTotal(deviceOlineTotal);
        deviceTotalVo.setDeviceOfflineTotal(deviceAccessTotal-deviceOlineTotal);
        deviceTotalVo.setDeviceAlarmTotal(deviceAlarmTotal);
        deviceTotalVo.setFactoryTotal(2);
        return deviceTotalVo;
    }

    @Override
    public void countTheDailyDataVolume(Date nowDate) {
        if (Objects.isNull(nowDate)){
            nowDate  = DateUtils.getNowDate();
        }
        // 生成日期集合 一次算7个点
        List<Date> dateListByTimesTamp = DateUtils.getDateListByTimesTamp(nowDate);
        for (Date date : dateListByTimesTamp) {
            //相同天则覆盖redis里面的值
            boolean isExit = RedisUtil.KeyOps.hasKey("device:everyDay:amount:" + DateUtils.getYearMonthDayByDate(date));
            //判断日期是否当前天
            if (DateUtils.isSameDay(date,nowDate)){
                // 不是当前天则查询redis里面有值就不在更新了
                // 获取设备每日采集量统计
                int amount = getAmount(date);
                RedisUtil.StringOps.set("device:everyDay:amount:"+DateUtils.getYearMonthDayByDate(date),amount+"");
            }
            if (!isExit){
                int amount = getAmount(date);
                RedisUtil.StringOps.set("device:everyDay:amount:"+DateUtils.getYearMonthDayByDate(date),amount+"");
            }
        }
    }

    @Override
    public DeviceDataCollectionVolumeVo dataCollectionVolume() {
        // 生成日期集合 一次算7个点collect
        List<Date> collect = DateUtils.getDateListByTimesTamp(DateUtils.getNowDate());
        List<Date> dateListByTimesTamp = collect.stream().sorted(Comparator.comparing(Date::getTime)).collect(Collectors.toList());
        List<String> volumeDate = new ArrayList<>();
        List<Integer> volumeValue = new ArrayList<>();
        for (Date date : dateListByTimesTamp) {
            //获取设备每日采集量统计
            String s = RedisUtil.StringOps.get("device:everyDay:amount:" + DateUtils.getYearMonthDayByDate(date));
            int amount = Integer.parseInt(Objects.isNull(s)?"0":s);
            volumeDate.add(DateUtils.getMonthDayByDate(date));
            volumeValue.add(amount);
        }
        DeviceDataCollectionVolumeVo deviceDataCollectionVolumeVo = new DeviceDataCollectionVolumeVo();
        deviceDataCollectionVolumeVo.setDataCollectionVolumeDate(volumeDate);
        deviceDataCollectionVolumeVo.setDataCollectionVolumeValue(volumeValue);
        return deviceDataCollectionVolumeVo;
    }

    /***
     * 获取设备每日采集量
     * @param date
     * @return
     */
    private int getAmount(Date date) {
        Integer amountOfCollection = iDeviceStateLogsService.getDeviceStateLogsData(date);
        Integer amountEnergy = iDeviceEnergyService.getDeviceEnergyData(date);
        Integer amountParameter = iDeviceParameterRecordService.getDeviceParameterData(date);
        Integer amountYields = iDeviceYieldsService.getDeviceYieldsData(date);
        int amount = amountOfCollection + amountEnergy + amountParameter + amountYields;
        return amount;
    }
}
